import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.hssf.record.SSTRecord;

public class ReadingExcelXlsUtil implements HSSFListener {
	private SSTRecord sstrec;

	/**
	 * This method listens for incoming records and handles them as required.
	 * 
	 * @param record
	 *            The record that was found while reading.
	 */

	@Override
	public void processRecord(org.apache.poi.hssf.record.Record arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getSid()) {
		// the BOFRecord can represent either the beginning of a sheet or the
		// workbook
		case BOFRecord.sid:
			BOFRecord bof = (BOFRecord) arg0;
			if (bof.getType() == bof.TYPE_WORKBOOK) {
				System.out.println("Encountered workbook");
				// assigned to the class level member
			} else if (bof.getType() == bof.TYPE_WORKSHEET) {
				System.out.println("Encountered sheet reference");
			}
			break;
		case BoundSheetRecord.sid:
			BoundSheetRecord bsr = (BoundSheetRecord) arg0;
			System.out.println("New sheet named: " + bsr.getSheetname());
			break;
		case RowRecord.sid:
			RowRecord rowrec = (RowRecord) arg0;
			System.out.println("Row found, first column at "
					+ rowrec.getFirstCol() + " last column at "
					+ rowrec.getLastCol() + " row number " + rowrec.getRowNumber());
			break;
		case NumberRecord.sid:
			NumberRecord numrec = (NumberRecord) arg0;
			System.out.println("Cell found with value " + numrec.getValue()
					+ " at row " + numrec.getRow() + " and column "
					+ numrec.getColumn());
			break;
		// SSTRecords store a array of unique strings used in Excel.
		case SSTRecord.sid:
			sstrec = (SSTRecord) arg0;
			for (int k = 0; k < sstrec.getNumUniqueStrings(); k++) {
				System.out.println("String table value " + k + " = "
						+ sstrec.getString(k));
			}
			break;
		case LabelSSTRecord.sid:
			LabelSSTRecord lrec = (LabelSSTRecord) arg0;
			System.out.println("String cell found with value "
					+ sstrec.getString(lrec.getSSTIndex()));
			break;
		}
	}
}
